<template>
	<view class="content u-cb-white-bg plr-100 pt-100">
		<!-- tabs -->
		<!-- <u-subsection 
			:list="['商家登录','会员登录']" 
			:current="currentIndex" 
			mode="subsection"
			fontSize="16"
			activeColor="#F56C6C"
			@change="onSectionChange"/> -->
		<view class="u-text-align-center fsize-34 mt-300">欢迎登录</view>
		<view class="mt-100">
			<u-form 
				:model="form" 
				:rules="rules"
				ref="uForm"
				:errorType="['border-bottom']">
				<u-form-item prop="usercode" :borderBottom="true">
					<u-input v-model="form.usercode" :clearable="true" border="none"  placeholder="请输入账号" />
				</u-form-item>
				<u-form-item prop="password" :borderBottom="true">
					<u-input 
						:password="isShowPassword"
						v-model="form.password" 
						:clearable="true" 
						border='none'
						placeholder="请输入密码">
							<template slot="suffix">
								<u-icon 
									size="20" 
									:name="isShowPassword?'eye-fill':'eye'" 
									:color="isShowPassword?'#c0c4cc':'#F56C6C'" 
									@click="isShowPassword=!isShowPassword" />
							</template>
					</u-input>
				</u-form-item>
			</u-form>
			<view class="mt-40">
				<u-button 
					open-type="getPhoneNumber" 
					@getphonenumber="onSubmit" 
					type="error" 
					text="授权登录"/>
			</view>
		</view>
	</view>
</template>

<script>
	import {mapState, mapMutations} from 'vuex'
	export default{
		computed: {
			...mapState(['hasLogin,userInfo'])
		},
		data(){
			return {
				currentIndex: 0,
				isShowPassword: true,
				form:{
					appid:'',
					code:'',
					usercode:'',
					password:'',
					type: 0,
				},
				rules: {
					usercode: [
						{ required: true, message: '请输入账号', trigger: ['blur', 'change']}
					],
					password: [
						{ required: true, message: '请输入密码', trigger: ['blur', 'change']}
					]
				}
			}
		},
		onLoad() {
			
		},
		methods: {
			...mapMutations(['login']),
			// onSectionChange(index) {
			// 	this.currentIndex = index
			// 	this.form.type = index + 1;
			// },
			onSubmit(e) {
				let that = this
				that.form = {
					...that.form,
					encryptedCode: e.detail.code || '',
					encryptedData: e.detail.encryptedData || '',
					iv: e.detail.iv || ''
				}
				const accountInfo = uni.getAccountInfoSync();
				this.$refs.uForm.validate().then(res => {
					uni.login({
						provider: 'weixin',
						success: function (loginRes){
							that.form.code = loginRes.code,
							that.form.appid = accountInfo.miniProgram.appId
							that.$reqPost('/OnUserLogin',that.form).then(res=>{
								that.login(res.Data[0])
								if(res.Data[0].FLoginType === 1){
									uni.reLaunch({
										url: '/pages/home/index-business'
									});
								}else{
									uni.reLaunch({
										url: '/pages/home/index-member'
									});
								}
							})
						}
					})
				}).catch(errors => {
					// uni.$u.toast('校验失败')
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.content{
		height:100vh;
	}
</style>